﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Data;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using iBeauty.App.Common;
using iBeauty.BLL;
using iBeauty.BusinessObjects;
namespace iBeauty.App.UCControl
{
    public partial class PreferenceUc : DevExpress.XtraEditors.XtraUserControl
    {
        public PreferenceUc()
        {
            InitializeComponent();

        }
        PreferenceBLL service = new PreferenceBLL();
        Preference preference = new Preference();

        private void LoadRoundType()
        {
            var roundTypes = new Dictionary<byte, string>();
            roundTypes.Add((byte)EnumBeauty.RoundType.RoundDown, EnumBeauty.GetDescriptionFromEnumValue(EnumBeauty.RoundType.RoundDown));
            roundTypes.Add((byte)EnumBeauty.RoundType.RoundMid, EnumBeauty.GetDescriptionFromEnumValue(EnumBeauty.RoundType.RoundMid));
            roundTypes.Add((byte)EnumBeauty.RoundType.RoundUp, EnumBeauty.GetDescriptionFromEnumValue(EnumBeauty.RoundType.RoundUp));

            cbbRoundTypeIn.DataSource = new BindingSource(roundTypes, null);

            cbbRoundTypeIn.ValueMember = "Key";
            cbbRoundTypeIn.DisplayMember = "Value";

            cbbRoundTypeOut.DataSource = new BindingSource(roundTypes, null);

            cbbRoundTypeOut.ValueMember = "Key";
            cbbRoundTypeOut.DisplayMember = "Value";
        }

        private void LoadShopType()
        {
            var shopTypes = new Dictionary<byte, string>();
            shopTypes.Add((byte)EnumBeauty.ShopType.Spa, EnumBeauty.GetDescriptionFromEnumValue(EnumBeauty.ShopType.Spa));
            shopTypes.Add((byte)EnumBeauty.ShopType.HairSalon, EnumBeauty.GetDescriptionFromEnumValue(EnumBeauty.ShopType.HairSalon));
            shopTypes.Add((byte)EnumBeauty.ShopType.BeatySalon, EnumBeauty.GetDescriptionFromEnumValue(EnumBeauty.ShopType.BeatySalon));

            cboType.DataSource = new BindingSource(shopTypes, null);

            cboType.ValueMember = "Key";
            cboType.DisplayMember = "Value";
        }

        private DataTable LoadStoreName()
        {
            return service.LoadStoreName();
        }
        private bool isValid()
        {

            if (txtCompanyName.Text == "")
            {
                MessageBox.Show("Bạn chưa nhập tên công ty");
                return false;
            }
            if (txtAddress.Text == "")
            {
                MessageBox.Show("Bạn chưa nhập địa chỉ công ty");
                return false;
            }
            if (txtTel.Text == "")
            {
                MessageBox.Show("Bạn chưa nhập số điện thoại công ty");
                return false;
            }
            if (txtFax.Text == "")
            {
                MessageBox.Show("Bạn chưa nhập số Fax công ty");
                return false;
            }
            if (txtEmail.Text == "")
            {
                MessageBox.Show("Bạn chưa nhập Email công ty");
                return false;
            }
            if (txtWebsite.Text == "")
            {
                MessageBox.Show("Bạn chưa nhập Website công ty");
                return false;
            }
            string email = null;
            email = "^([0-9a-zA-Z]([-\\.\\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]\\.)+[a-zA-Z]{2,9})$";

            if (!(Regex.IsMatch(txtEmail.Text, email)))
            {
                MessageBox.Show("Not a Valid Email address ");
                return false;
            }
            string urlchk = null;
            urlchk = @"((file|gopher|news|nntp|telnet|http|ftp|https|ftps|sftp|www)://)+(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,15})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(/[a-zA-Z0-9\&amp;%_\./-~-]*)?";
            if (!(Regex.IsMatch(txtWebsite.Text, urlchk)))
            {
                MessageBox.Show("Not a Valid URL ");
                return false;
            }
            return true;
        }

        private void LoadData()
        {
            preference = service.GetPreferences1();
            txtCompanyName.Text = preference.CompanyName;
            txtAddress.Text = preference.Address;
            txtTel.Text = preference.Phone;
            txtFax.Text = preference.Fax;
            txtEmail.Text = preference.Email;
            txtWebsite.Text = preference.Website;
            cboStoreName.DataSource = LoadStoreName();
            cboStoreName.DisplayMember = "ShopName";
            cboStoreName.ValueMember = "ShopCode";
        }
        private void xtraTabPreference_SelectedPageChanged(object sender, DevExpress.XtraTab.TabPageChangedEventArgs e)
        {

            switch (xtraTabPreference.SelectedTabPage.Name)
            {
                case "xtraTabInfomation":
                    {
                        LoadData();
                        LoadRoundType();
                        LoadShopType();
                        break;
                    }
            }
        }

        private void PreferenceUc_Load(object sender, EventArgs e)
        {
            xtraTabPreference.SelectedTabPageIndex = 1;
            xtraTabPreference.SelectedTabPage = xtraTabInfomation;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (isValid())
            {
                preference.CompanyName = txtCompanyName.Text;
                preference.Address = txtAddress.Text;
                preference.Email = txtEmail.Text;
                preference.Phone = txtTel.Text;
                preference.Fax = txtFax.Text;
                preference.Website = txtWebsite.Text;
                preference.ShopName = cboStoreName.Text;
                preference.ShopCode = cboStoreName.SelectedValue.ToString();
                preference.StartDate = DateTime.Parse(startDate.Text);
                preference.EndDate = DateTime.Parse(endDate.Text);
                service.AddPreference(preference);
            }
        }

        private void btnCancle_Click(object sender, EventArgs e)
        {
            txtCompanyName.Text = "";
            txtAddress.Text = "";
            txtEmail.Text = "";
            txtFax.Text = "";
            txtTel.Text = "";
            txtWebsite.Text = "";
            txtCompanyName.Focus();

        }

        private void txtTel_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                MessageBox.Show("Chỉ được nhập kí tự số");
                e.Handled = true;
            }
        }

        private void txtFax_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                MessageBox.Show("Chỉ được nhập kí tự số");
                e.Handled = true;
            }
        }
    }
}